package com.hwhhhh.mygrad.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hwhhhh.mygrad.AdminActivity;
import com.hwhhhh.mygrad.R;
import com.hwhhhh.mygrad.dto.FreeClsDTO;
import com.hwhhhh.mygrad.fragment.ClsEditFragment;
import com.hwhhhh.mygrad.fragment.HintFragment;
import com.hwhhhh.mygrad.utils.StringUtils;
import com.hwhhhh.mygrad.view.IconfontView;

import java.util.List;

public class AdminClsAdapter extends RecyclerView.Adapter<AdminClsAdapter.AdminClsHolder> {
    private List<FreeClsDTO> data;
    private Context mContext;

    public AdminClsAdapter(Context context, List<FreeClsDTO> data) {
        this.mContext = context;
        this.data = data;
    }

    @NonNull
    @Override
    public AdminClsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_admin_cls, parent, false);
        return new AdminClsHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull AdminClsHolder holder, int position) {
        FreeClsDTO entity = data.get(position);
        String str = entity.getCampus() + "-" + entity.getClassroomNum();
        holder.clsNum.setText(str);
        holder.delete.setOnClickListener(v -> {
            HintFragment hintFragment = HintFragment.newInstance("提示", "您确定要删除吗？", null, null, AdminActivity.DELETE_FRAG_CODE);
            Bundle bundle = new Bundle();
            bundle.putInt("position", position);
            bundle.putSerializable("freeCls", entity);
            hintFragment.setBundle(bundle);
            hintFragment.show(((AdminActivity)mContext).getSupportFragmentManager(), HintFragment.class.getSimpleName());
        });
        boolean[] clsArr = new boolean[13];
        for (char c : entity.getFreeCls().toCharArray()) {
            int cur = StringUtils.clsChToInt(c);
            clsArr[cur - 1] = true;
        }
        holder.edit.setOnClickListener(v -> {
            String title = "编辑" + entity.getClassroomNum();
            ClsEditFragment fragment = ClsEditFragment.newInstance(title, clsArr, AdminActivity.EDIT_FRAG_CODE);
            fragment.show(((AdminActivity)mContext).getSupportFragmentManager(), ClsEditFragment.class.getSimpleName());
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class AdminClsHolder extends RecyclerView.ViewHolder {
        TextView clsNum;
        IconfontView edit, delete;

        public AdminClsHolder(@NonNull View itemView) {
            super(itemView);
            clsNum = itemView.findViewById(R.id.tv_cls_num);
            edit = itemView.findViewById(R.id.ic_edit);
            delete = itemView.findViewById(R.id.ic_delete);
        }
    }

    public void remove(int position) {
        this.data.remove(position);
    }
}
